<?php

namespace SwooleCrontab;

use \Swoole\Timer as SwooleTimer;
use \Swoole\Event as SwooleEvent;
use \Swoole\Http\Request as Request;
use \Swoole\Http\Response as Response;

$http = new \Swoole\Http\Server("127.0.0.1", 9501);

SwooleTimer::tick(1000, function (int $timer_id) {

    // 执行规则 ， 任务command  ，任务中的参数，任务状态[是否正在执行]
    // 操作: 编辑任务,删除任务,暂停任务(执行期间强制终止进程；下一次禁止执行）

    //日志系统:存储，展示（检索，搜索），删除日志，日志导入es 等等分析工具

    //

    var_dump(SwooleTimer::stats($timer_id));

    echo "mypid is ".getmypid()."  " .date("Y-m-d H:i:s") . "  " . "    -   aaa" . rand(1, 9999) . PHP_EOL;
    //获取所有的人物列表
});
SwooleTimer::tick(5000,function(int $timer_id){
    var_dump(SwooleTimer::stats($timer_id));
    echo "mypid is ".getmypid()." ".date("Y-m-d H:i:s")." 5 s begin".PHP_EOL;
});
$task_array = SwooleTimer::list();
$http->on('request', function (Request $request, Response $response)use($task_array) {


    var_dump($request->server["request_uri"]);

    $response->header("Content-Type", "application/json");



    $url = $request->server["request_uri"];
    if ($url == "/lists"){
        SwooleTimer::tick(3000,function($timer_id){
            echo "create tick:".$timer_id.PHP_EOL;

        });
        foreach($task_array as $timer_id){
            var_dump($timer_id);
            $result = SwooleTimer::stats($timer_id);
            var_dump($result);

            //var_dump(SwooleTimer::info($timer_id));
        }

        $task_arrays = SwooleTimer::List();
        var_dump($task_arrays);

    }else{
        //开启一个定时任务

    }
    //10s以后清理定时器
    //SwooleTimer::clear($new_tick_id);
    $response->end(json_encode($task_array));
});
$http->start();

//swoole 定时器

/*Swoole\Timer::tick(1000,function(){
        echo date("Y-m-d H:i:s")."    -   aaa".rand(1,9999).PHP_EOL;
});*/

//每五秒执行一次


//* * * * * * 秒，分时日月周
//1 1 3 * * * 每天3点1分1秒
